﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.Common;
using BestBuy.DAL;

namespace BestBuy.PageGuest
{
    public partial class PageContactMessage : System.Web.UI.Page
    {
        private CCongTy _ct = new CCongTy();
        private CProduct _csp = new CProduct();
        private CBuyerOffer _cb = new CBuyerOffer();
        private CSellerOffer _cs = new CSellerOffer();
        private CDanhMuc _cdm = new CDanhMuc();
        private CMessage _csms = new CMessage();
        private CThongTinCaNhan _cttcn = new CThongTinCaNhan();
        private BMessage sms = new BMessage();
        private CNhanVien _cnv = new CNhanVien();
        protected void Page_Load(object sender, EventArgs e)
        {
            pnError.Visible = false;
            pnContact.Visible = false;
            GetParams();
            LoadLinkQuick();
        }

        protected void GetParams()
        {
            if (Session["IDDN"].ToString().Equals(""))
            {
                string duongdan = "~/Account/Login.aspx?ml=" + Request.Params["ml"].ToString() + "&pid=" + Request.Params["pid"].ToString();
                Response.Redirect(duongdan);
                return;
            }
            if (Request.Params["ml"] != null && Request.Params["pid"] != null)
            {
                try
                {
                    if (Session["LoaiTK"].ToString().Equals("3") || Session["LoaiTK"].ToString().Equals("4"))
                    {
                        btnSend.Enabled = false;
                    }
                    else
                    {
                        btnSend.Enabled = true;
                    }
                    sms.IDTKSender = Convert.ToInt32(Session["IDDN"].ToString());
                    sms.Source = Convert.ToInt32(Request.Params["ml"].ToString());
                    sms.IDContact = Convert.ToInt32(Request.Params["pid"].ToString());
                }
                catch
                {
                    Response.Redirect("~/PageGuest/PageError.aspx");
                }
            }
        }

        private void HyperLinkQuick(int id)
        {
            DanhMuc dm = new DanhMuc();
            dm = _cdm.GetByID(id);
            if (dm.IDDanhMucCha != null)
            {
                DanhMuc dmparent = new DanhMuc();
                dmparent = _cdm.GetByID((int)dm.IDDanhMucCha);
                HyperLink hl = new HyperLink();
                hl.ID = "hll" + id.ToString();
                if (dmparent.IDDanhMucCha != null)
                {
                    hl.NavigateUrl = "~/PageGuest/ListProductOfCategory.aspx?pid=" + dm.IDDanhMucCha;
                }
                else
                {
                    hl.NavigateUrl = "~/PageGuest/ListOfCategoryLevel1.aspx?pid=" + dm.IDDanhMucCha;
                }
                hl.Text = dmparent.TenDanhMuc;
                HyperLinkQuick((int)dm.IDDanhMucCha);
                linkQuick.Controls.Add(hl);
                Label lb = new Label();
                lb.Text = " > ";
                lb.ID = "lbb" + dm.IDDanhMuc.ToString();
                linkQuick.Controls.Add(lb);
            }
        }

        private void CheckMessage(string ten)
        {
            BMessage bsms = _csms.GetMessageExists(sms.IDTKSender, sms.IDTKReceiver, sms.IDContact, sms.Source);
            if (bsms != null)
            {
                pnContact.Visible = true;
                lbNoiDung.Text = bsms.Date + " - " + ten + " : " + bsms.Subject;
            }
        }

        private void CheckMessageMuti(int idgui, int idnhan, int idcontact, int source, string ten)
        {
            BMessage bsms = _csms.GetMessageExists(idgui, idnhan, idcontact, source);
            if (bsms != null)
            {
                pnContact.Visible = true;
                lbNoiDung.Text += bsms.Date + " - " + ten + " : " + bsms.Subject + "<br />";
            }
        }

        protected void LoadLinkProduct()
        {
            HyperLink hl = new HyperLink();
            hl.ID = "hlCap1";
            hl.NavigateUrl = "~/PageGuest/ListCategory.aspx";
            hl.Text = "Products";
            linkQuick.Controls.Add(hl);

            Label lbp = new Label();
            lbp.Text = " > ";
            lbp.ID = "lbp" + sms.IDContact.ToString();
            linkQuick.Controls.Add(lbp);

            //load thong tin
            if (Session["LoaiTK"].ToString().Equals("1") || Session["LoaiTK"].ToString().Equals("2"))
            {
                ThongTinTaiKhoan tk = _cttcn.GetAccountID(sms.IDTKSender);
                lbPerson.Text = tk.HoTen;
                lbCompanyContact.Text = "< " + tk.CongTy.TenCongTy + " >";
            }
            else {
                NhanVien nv = _cnv.GetAccountID(sms.IDTKSender);
                lbPerson.Text = nv.HoTen;
                lbCompanyContact.Text = "<  >";
            }

            if (sms.IDContact > 0)
            {

                //load linkQuick
                SanPham sp = _csp.GetProductID(sms.IDContact);

                HyperLinkQuick((int)sp.IDDanhMuc);
                HyperLink hlp = new HyperLink();
                hlp.ID = "hlp" + sp.IDDanhMuc.ToString();
                hlp.NavigateUrl = "~/PageGuest/ListProductOfCategory.aspx?pid=" + sp.IDDanhMuc;
                hlp.Text = sp.DanhMuc.TenDanhMuc;
                linkQuick.Controls.Add(hlp);

                //load ten san pham
                Label lbn = new Label();
                lbn.Text = " > ";
                lbn.ID = "lbn" + sms.IDContact.ToString();
                linkQuick.Controls.Add(lbn);
                Label lbnext = new Label();
                lbnext.Text = sp.TenSanPham;
                lbnext.ID = "lbnext" + sp.IDSanPham;
                linkQuick.Controls.Add(lbnext);

                HyperLink hlContact = new HyperLink();
                hlContact.ID = "hlContact" + sms.IDContact;
                hlContact.Text = sp.TenSanPham;
                hlContact.NavigateUrl = "~/PageGuest/ProductDetail.aspx?pid=" + sp.IDSanPham;
                hlContact.Target = "_blank";
                linkContact.Controls.Add(hlContact);

                sms.IDTKReceiver = sp.ThongTinTaiKhoan.IDTaiKhoan;

                if (sms.IDTKReceiver == sms.IDTKSender) { btnSend.Enabled = false; hlContact.CssClass = "message_noacction"; }
                

                CheckMessage(sp.TenSanPham);

            }
            else if (sms.IDContact == 0)
            { //co nhiu dong du lieu
                Label mutiName = new Label();
                mutiName.ID = "mutiName";
                int dem = 0;
                foreach (SanPham pd in CListProduct.list)
                {
                    HyperLink mutiContact = new HyperLink();
                    mutiContact.ID = "mutiContact" + pd.IDSanPham;
                    mutiContact.Text = pd.TenSanPham + "<br />";
                    mutiContact.NavigateUrl = "~/PageGuest/ProductDetail.aspx?pid=" + pd.IDSanPham;
                    mutiContact.Target = "_blank";

                    linkContact.Controls.Add(mutiContact);

                    if (dem == CListProduct.list.Count - 1)
                    {
                        mutiName.Text += pd.TenSanPham;
                    }
                    else
                    {
                        mutiName.Text += pd.TenSanPham + " ,";
                    }
                    if (sms.IDTKSender == pd.IDTaiKhoan)
                    {
                        mutiContact.CssClass = "message_noacction";
                    }

                    CheckMessageMuti(sms.IDTKSender, pd.IDTaiKhoan, pd.IDSanPham, sms.Source, pd.TenSanPham);
                    dem++;
                }

                linkQuick.Controls.Add(mutiName);
            }
        }

        protected void LoadLinkBuyOffer()
        {
            HyperLink hl = new HyperLink();
            hl.ID = "hlCap1";
            hl.NavigateUrl = "~/PageGuest/bListCategory.aspx";
            hl.Text = "Buy Offers";
            Label lbb = new Label();
            lbb.Text = " > ";
            lbb.ID = "lbb" + sms.IDContact.ToString();
            linkQuick.Controls.Add(hl);
            linkQuick.Controls.Add(lbb);

            //load thong tin
            if (Session["LoaiTK"].ToString().Equals("1") || Session["LoaiTK"].ToString().Equals("2"))
            {
                ThongTinTaiKhoan tk = _cttcn.GetAccountID(sms.IDTKSender);
                lbPerson.Text = tk.HoTen;
                lbCompanyContact.Text = "< " + tk.CongTy.TenCongTy + " >";
            }
            else
            {
                NhanVien nv = _cnv.GetAccountID(sms.IDTKSender);
                lbPerson.Text = nv.HoTen;
                lbCompanyContact.Text = "<  >";
            }

            if (sms.IDContact > 0)
            {
                //load linkQuick
                DanhMuc danhmuc = _cb.GetCatalogyOfIDBangTinMua(sms.IDContact);
                HyperLink hlb = new HyperLink();
                hlb.ID = "hl" + sms.IDContact.ToString();
                hlb.NavigateUrl = "~/PageGuest/bListOfCategoryLevel1.aspx?pid=" + danhmuc.IDDanhMuc;
                hlb.Text = danhmuc.TenDanhMuc;
                linkQuick.Controls.Add(hlb);

                BangTinMua sp = _cb.GetBuyerOfferID(sms.IDContact);
                Label lb = new Label();
                lb.ID = "lbTieuDe" + sms.IDContact.ToString();
                lb.Text = " > " + sp.TieuDe;
                linkQuick.Controls.Add(lb);

                HyperLink hlContact = new HyperLink();
                hlContact.ID = "hlContact" + sms.IDContact;
                hlContact.Text = sp.TieuDe;
                hlContact.NavigateUrl = "~/PageGuest/bBuyOfferDetail.aspx?pid=" + sp.IDBangTinMua;
                hlContact.Target = "_blank";
                linkContact.Controls.Add(hlContact);

                sms.IDTKReceiver = sp.ThongTinTaiKhoan.IDTaiKhoan;

                if (sms.IDTKReceiver == sms.IDTKSender) { btnSend.Enabled = false; hlContact.CssClass = "message_noacction"; }
                

                CheckMessage(sp.TieuDe);
            }
            else if (sms.IDContact == 0)
            { //co nhiu dong du lieu
                Label mutiName = new Label();
                mutiName.ID = "mutiName";
                int dem = 0;
                foreach (BangTinMua pd in CListBuy.list)
                {
                    HyperLink mutiContact = new HyperLink();
                    mutiContact.ID = "mutiContact" + pd.IDBangTinMua;
                    mutiContact.Text = pd.TieuDe + "<br />";
                    mutiContact.NavigateUrl = "~/PageGuest/ProductDetail.aspx?pid=" + pd.IDBangTinMua;
                    mutiContact.Target = "_blank";
                    linkContact.Controls.Add(mutiContact);
                    if (dem == CListBuy.list.Count - 1)
                    {
                        mutiName.Text += pd.TieuDe;
                    }
                    else
                    {
                        mutiName.Text += pd.TieuDe + " ,";
                    }
                    if (sms.IDTKSender == pd.IDTaiKhoan)
                    {
                        mutiContact.CssClass = "message_noacction";
                    }
                    CheckMessageMuti(sms.IDTKSender, pd.IDTaiKhoan, pd.IDBangTinMua, sms.Source, pd.TieuDe);
                    dem++;
                }
                linkQuick.Controls.Add(mutiName);
            }
        }

        protected void LoadLinkSellOffer()
        {
            HyperLink hl = new HyperLink();
            hl.ID = "hlCap1";
            hl.NavigateUrl = "~/PageGuest/sListCategory.aspx";
            hl.Text = "Sell Offers";
            Label lbs = new Label();
            lbs.Text = " > ";
            lbs.ID = "lbs" + sms.IDContact.ToString();
            linkQuick.Controls.Add(hl);
            linkQuick.Controls.Add(lbs);


            //load thong tin
            if (Session["LoaiTK"].ToString().Equals("1") || Session["LoaiTK"].ToString().Equals("2"))
            {
                ThongTinTaiKhoan tk = _cttcn.GetAccountID(sms.IDTKSender);
                lbPerson.Text = tk.HoTen;
                lbCompanyContact.Text = "< " + tk.CongTy.TenCongTy + " >";
            }
            else
            {
                NhanVien nv = _cnv.GetAccountID(sms.IDTKSender);
                lbPerson.Text = nv.HoTen;
                lbCompanyContact.Text = "<  >";
            }

            if (sms.IDContact > 0)
            {
                //load linkQuick
                DanhMuc danhmuc = _cs.GetCatalogyOfIDBangTinBan(sms.IDContact);
                HyperLink hls = new HyperLink();
                hls.ID = "hl" + sms.IDContact.ToString();
                hls.NavigateUrl = "~/PageGuest/sListOfCategoryLevel1.aspx?pid=" + danhmuc.IDDanhMuc;
                hls.Text = danhmuc.TenDanhMuc;
                linkQuick.Controls.Add(hls);

                BangTinBan sp = _cs.GetSellOfferID(sms.IDContact);
                Label lb = new Label();
                lb.ID = "lbTieuDe" + sms.IDContact.ToString();
                lb.Text = " > " + sp.TieuDe;
                linkQuick.Controls.Add(lb);

                HyperLink hlContact = new HyperLink();
                hlContact.ID = "hlContact" + sms.IDContact;
                hlContact.Text = sp.TieuDe;
                hlContact.NavigateUrl = "~/PageGuest/sSellOffersDetail.aspx?pid=" + sp.IDBangTinBan;
                hlContact.Target = "_blank";
                linkContact.Controls.Add(hlContact);

                sms.IDTKReceiver = sp.ThongTinTaiKhoan.IDTaiKhoan;

                if (sms.IDTKReceiver == sms.IDTKSender) { btnSend.Enabled = false; hlContact.CssClass = "message_noacction"; }
                

                CheckMessage(sp.TieuDe);
            }
            else if (sms.IDContact == 0)
            { //co nhiu dong du lieu
                Label mutiName = new Label();
                mutiName.ID = "mutiName";
                int dem = 0;
                foreach (BangTinBan pd in CListSell.list)
                {
                    HyperLink mutiContact = new HyperLink();
                    mutiContact.ID = "mutiContact" + pd.IDBangTinBan;
                    mutiContact.Text = pd.TieuDe + "<br />";
                    mutiContact.NavigateUrl = "~/PageGuest/ProductDetail.aspx?pid=" + pd.IDBangTinBan;
                    mutiContact.Target = "_blank";
                    linkContact.Controls.Add(mutiContact);
                    if (dem == CListSell.list.Count - 1)
                    {
                        mutiName.Text += pd.TieuDe;
                    }
                    else
                    {
                        mutiName.Text += pd.TieuDe + " ,";
                    }
                    if (sms.IDTKSender == pd.IDTaiKhoan)
                    {
                        mutiContact.CssClass = "message_noacction";
                    }
                    CheckMessageMuti(sms.IDTKSender, pd.IDTaiKhoan, pd.IDBangTinBan, sms.Source, pd.TieuDe);
                    dem++;
                }
                linkQuick.Controls.Add(mutiName);
            }
        }

        protected void LoadLinkCompany()
        {
            HyperLink hl = new HyperLink();
            hl.ID = "hlCap1";
            hl.NavigateUrl = "~/PageGuest/cListCategory.aspx";
            hl.Text = "Companies";
            Label lbc = new Label();
            lbc.Text = " > ";
            lbc.ID = "lbc" + sms.IDContact.ToString();
            linkQuick.Controls.Add(hl);
            linkQuick.Controls.Add(lbc);
            //load linkQuick
            DanhMuc danhmuc = _ct.GetCatalogyOfIDCompany(sms.IDContact);
            HyperLink hlc = new HyperLink();
            hlc.ID = "hl" + sms.IDContact.ToString();
            hlc.NavigateUrl = "~/PageGuest/sListOfCategoryLevel1.aspx?pid=" + danhmuc.IDDanhMuc;
            hlc.Text = danhmuc.TenDanhMuc;
            linkQuick.Controls.Add(hlc);

            ThongTinTaiKhoan tk = _cttcn.GetAcountOfCompany(sms.IDContact);
            Label lb = new Label();
            lb.ID = "lbTenCongTy" + sms.IDContact.ToString();
            lb.Text = " > " + tk.CongTy.TenCongTy;
            linkQuick.Controls.Add(lb);

            //load thong tin
            
            if (Session["LoaiTK"].ToString().Equals("1") || Session["LoaiTK"].ToString().Equals("2"))
            {
                ThongTinTaiKhoan tkr = _cttcn.GetAccountID(sms.IDTKSender);
                lbPerson.Text = tkr.HoTen;
                lbCompanyContact.Text = "< " + tkr.CongTy.TenCongTy + " >";//load thong tin
            }
            else
            {
                NhanVien nv = _cnv.GetAccountID(sms.IDTKSender);
                lbPerson.Text = nv.HoTen;
                lbCompanyContact.Text = "<  >";
            }

            HyperLink hlContact = new HyperLink();
            hlContact.ID = "hlContact" + sms.IDContact;
            hlContact.Text = tk.CongTy.TenCongTy;
            hlContact.NavigateUrl = "~/PageGuest/cCompanyProfile.aspx?pid=" + tk.IDCongTy;
            hlContact.Target = "_blank";
            linkContact.Controls.Add(hlContact);

            sms.IDTKReceiver = tk.IDTaiKhoan;

            if (sms.IDTKReceiver == sms.IDTKSender) { btnSend.Enabled = false; hlContact.CssClass = "message_noacction"; }
            

            CheckMessage(tk.CongTy.TenCongTy);
        }

        protected void LoadLinkQuick()
        {

            if (sms.Source == (int)SourceContactStatus.Product) { LoadLinkProduct(); }
            else if (sms.Source == (int)SourceContactStatus.BuyOffer) { LoadLinkBuyOffer(); }
            else if (sms.Source == (int)SourceContactStatus.SellOffer) { LoadLinkSellOffer(); }
            else if (sms.Source == (int)SourceContactStatus.Company) { LoadLinkCompany(); }
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            bool kq = false;
            if (sms.IDContact > 0)
            { //chi 1
                sms.Subject = txtMessage.Text;
                sms.Status = (int)MessageStatus.SentUnread;
                sms.Date = DateTime.Now.ToString("MM/dd/yyyy");
                if (!txtRequiredQuantity.Text.Equals(""))
                {
                    sms.RequiredQuantity = Convert.ToInt32(txtRequiredQuantity.Text);
                }
                kq = _csms.InsertMessage(sms);
                if (sms.Source == (int)SourceContactStatus.Product && kq)
                {
                    kq = _csp.UpdateLuotContact(sms.IDContact);
                }
            }
            else if (sms.IDContact == 0)
            { // chua co IDContact va IDTKReceiver
                List<BMessage> listsms = new List<BMessage>();
                if (sms.Source == (int)SourceContactStatus.Product)
                {
                    foreach (SanPham sp in CListProduct.list)
                    {
                        int idsender = Convert.ToInt32(Session["IDDN"].ToString());
                        if (idsender != sp.IDTaiKhoan)
                        {
                            BMessage bsms = new BMessage();
                            bsms.IDContact = sp.IDSanPham;
                            bsms.IDTKReceiver = sp.IDTaiKhoan;
                            bsms.Date = DateTime.Now.ToString("MM/dd/yyyy");
                            bsms.Status = (int)MessageStatus.SentUnread;
                            bsms.Subject = txtMessage.Text;
                            bsms.IDTKSender = idsender;
                            bsms.Source = Convert.ToInt32(Request.Params["ml"].ToString());
                            if (!txtRequiredQuantity.Text.Equals(""))
                            {
                                bsms.RequiredQuantity = Convert.ToInt32(txtRequiredQuantity.Text);
                            }
                            listsms.Add(bsms);
                        }
                    }
                }
                else if (sms.Source == (int)SourceContactStatus.BuyOffer)
                {
                    foreach (BangTinMua sp in CListBuy.list)
                    {
                        int idsender = Convert.ToInt32(Session["IDDN"].ToString());
                        if (idsender != sp.IDTaiKhoan)
                        {
                            BMessage bsms = new BMessage();
                            bsms.IDContact = sp.IDBangTinMua;
                            bsms.IDTKReceiver = sp.IDTaiKhoan;
                            bsms.Date = DateTime.Now.ToString("MM/dd/yyyy");
                            bsms.Status = (int)MessageStatus.SentUnread;
                            bsms.Subject = txtMessage.Text;
                            bsms.IDTKSender = idsender;
                            bsms.Source = Convert.ToInt32(Request.Params["ml"].ToString());
                            if (!txtRequiredQuantity.Text.Equals(""))
                            {
                                bsms.RequiredQuantity = Convert.ToInt32(txtRequiredQuantity.Text);
                            }
                            listsms.Add(bsms);
                        }
                    }
                }
                else if (sms.Source == (int)SourceContactStatus.SellOffer)
                {

                    foreach (BangTinBan sp in CListSell.list)
                    {
                        int idsender = Convert.ToInt32(Session["IDDN"].ToString());
                        if (idsender != sp.IDTaiKhoan)
                        {
                            BMessage bsms = new BMessage();
                            bsms.IDContact = sp.IDBangTinBan;
                            bsms.IDTKReceiver = sp.IDTaiKhoan;
                            bsms.Date = DateTime.Now.ToString("MM/dd/yyyy");
                            bsms.Status = (int)MessageStatus.SentUnread;
                            bsms.Subject = txtMessage.Text;
                            bsms.IDTKSender = idsender;
                            bsms.Source = Convert.ToInt32(Request.Params["ml"].ToString());
                            if (!txtRequiredQuantity.Text.Equals(""))
                            {
                                bsms.RequiredQuantity = Convert.ToInt32(txtRequiredQuantity.Text);
                            }
                            listsms.Add(bsms);
                        }
                    }
                }
                if (listsms.Count > 0)
                {
                    kq = _csms.InsertAllMessage(listsms);
                    if (kq && sms.Source == (int)SourceContactStatus.Product) {
                        foreach (BMessage bsms in listsms) {
                            kq = _csp.UpdateLuotContact(bsms.IDContact);
                        }
                    }
                }
            }
            if (kq)
            {
                pnError.Visible = true;
                pnError.CssClass = "InLeft success message";
                lbError.Text = "Send message successfully.";
                txtMessage.Text = "";
                txtRequiredQuantity.Text = "";
            }
            else
            {
                pnError.Visible = true;
                pnError.CssClass = "InLeft error message";
                lbError.Text = "Send message failure.";
                txtMessage.Text = "";
                txtRequiredQuantity.Text = "";
            }
        }
    }
}